<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile_model extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
    public function createMember($data, $email, $password) {
    	$this->db->insert('members', $data);
    	$this->createUser($email, $password);
    	redirect('public/index');
    }
    
    public function createUser($email, $password) {
    	$password =do_hash($password, 'md5');
    	$query = $this->db->query("select id from members where email_id = '$email'");
    	//print_r($this->db->last_query()); exit;
    	$row = $query->row(); 
    	$id = $row->id;
    	$data = array(
    			'id' => $id,
    			'username' => $email,
    			'password' => $password,
    			'role' => 'normal');
    	$this->db->insert('users', $data);    	
    }
   
    public function show_profilebyemail($email) {
    	$this->load->database();
    	$this->db->select('*');
    	$this->db->from('members');
    	$this->db->join('dept', 'members.dept = dept.deptId');
    	$this->db->where('email_id', $email);
    	$query = $this->db->get();
    	return $query->result();
    }
    
    
    public function show_profile($id) {
    	$this->load->database();
    	$this->db->select('*');
    	$this->db->from('members');
    	$this->db->join('dept', 'members.dept = dept.deptId');
    	$this->db->where('id', $id);
    	$query = $this->db->get();
    	return $query->result();
    }
    
    public function edit_profile($id = '') {
    	//$ref= $this->security->xss_clean($this->input->post('ref'));
    	$name = $this->security->xss_clean($this->input->post('name'));
    	$batch = $this->security->xss_clean($this->input->post('batch'));
    	$comp = $this->security->xss_clean($this->input->post('comp'));
    	$loc = $this->security->xss_clean($this->input->post('loc'));
    	$design = $this->security->xss_clean($this->input->post('design'));
    	$mob = $this->security->xss_clean($this->input->post('mob'));
    	$mail = $this->security->xss_clean($this->input->post('mail'));
    	$uid = $this->security->xss_clean($this->input->post('uid'));
    	if($uid)
    		$id = $uid;
    	$data = array(
    			'name' => $name,
    			'batch' => $batch,
    			'current_company' => $comp,
    			'current_desig' => $design,
    			'curren_loc' => $loc,
    			'mobile' => $mob,
    			'email_id' => $mail
    	);
    	
    	$this->db->where('id', $id);
    	$this->db->update('members', $data);
    	
    	return $id;
    }
    
    public function send_mail() {
    	$id = $this->security->xss_clean($this->input->post('id'));
    	$yname = $this->security->xss_clean($this->input->post('yname'));
    	$yemail = $this->security->xss_clean($this->input->post('yemail'));
    	$name = $this->security->xss_clean($this->input->post('name'));
    	$req = $this->security->xss_clean($this->input->post('req'));
    	$query = $this->show_profile($id);
    	if(current($query)->email_id) {
	    	$this->email->from($yemail, $yname);
	    	$this->email->to(current($query)->email_id);
	    	$this->email->subject('Requesting For Contact');
	    	$this->email->message("Dear Alumni, <br> A person is requesting for your contact. Please respond to him/her on your own discretion <br>The message sent by ".$yname." is this <br><b>".$req."</b><br> Thank you!! <br> Alumni Website Administrator");
	    	if($this->email->send())
	    	{
	    		$this->session->set_flashdata('email_sent', 'Your mail has been sent');
	    		$this->common->sendAck($yemail, 'Re : Requesting For Contact');
	    	}
	    	else
	    		$this->session->set_flashdata('email_not_sent', 'Your mail has been sent');
    	}
    	
    }
    
}